﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vtron.LEDCorrective.UserControls
{
    /// <summary>
    /// CIEUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class CIEUserControl : UserControl
    {
        AxisPoints axisPoints = new AxisPoints();
        Line line0 = new Line();
        Line line1 = new Line();
        Line line2 = new Line();

        public Point[] MesaureAnglePoints = null;
        public CIEUserControl()
        {
            InitializeComponent();
            Points = new List<Point>();
            //axisPoints.NumofMousePress = 0;
            //假设实际测量的目标色域三角形型RGB色域值分别为 MesaureAnglePoints[0]、 MesaureAnglePoints[1]、MesaureAnglePoints[2];
            MesaureAnglePoints = new Point[3] { new Point(0.7037, 0.2599), new Point(0.0684, 0.8138), new Point(0.1710, 0.0208) };
            //测量的色坐标三角形坐标转换为屏幕坐标
            MesaureAnglePoints[0] = axisPoints.CIETransformToScreen(MesaureAnglePoints[0]);
            MesaureAnglePoints[1] = axisPoints.CIETransformToScreen(MesaureAnglePoints[1]);
            MesaureAnglePoints[2] = axisPoints.CIETransformToScreen(MesaureAnglePoints[2]);

            axisPoints.MeasureAngleAxis.AddRange(MesaureAnglePoints);
            axisPoints.TargetPointArray.AddRange(MesaureAnglePoints);

            Loaded += CIEUserControl_Loaded;
        }

        public List<Point> Points
        {
            get
            {
                List<Point> points = new List<Point>();
                Point point0 = axisPoints.ScreenTransformToCIE(axisPoints.TargetPointArray[0]);
                Point point1 = axisPoints.ScreenTransformToCIE(axisPoints.TargetPointArray[1]);
                Point point2 = axisPoints.ScreenTransformToCIE(axisPoints.TargetPointArray[2]);

                points.Add(point0);
                points.Add(point1);
                points.Add(point2);

                return points;
            }
            set
            {
                if (value.Count() == 3)
                {
                    MesaureAnglePoints[0] = axisPoints.CIETransformToScreen(value[0]);
                    MesaureAnglePoints[1] = axisPoints.CIETransformToScreen(value[1]);
                    MesaureAnglePoints[2] = axisPoints.CIETransformToScreen(value[2]);

                    axisPoints.TargetPointArray = MesaureAnglePoints.ToList();

                    canvas.Children.Remove(line0);
                    canvas.Children.Remove(line1);
                    canvas.Children.Remove(line2);

                    //绘制三角形
                    line0.Stroke = new SolidColorBrush(Colors.Black);
                    //line0.dr
                    line0.X1 = MesaureAnglePoints[0].X;
                    line0.Y1 = MesaureAnglePoints[0].Y;
                    line0.X2 = MesaureAnglePoints[1].X;
                    line0.Y2 = MesaureAnglePoints[1].Y;
                    canvas.Children.Add(line0);

                    line1.Stroke = new SolidColorBrush(Colors.Black);
                    line1.X1 = MesaureAnglePoints[1].X;
                    line1.Y1 = MesaureAnglePoints[1].Y;
                    line1.X2 = MesaureAnglePoints[2].X;
                    line1.Y2 = MesaureAnglePoints[2].Y;
                    canvas.Children.Add(line1);

                    line2.Stroke = new SolidColorBrush(Colors.Black);
                    line2.X1 = MesaureAnglePoints[2].X;
                    line2.Y1 = MesaureAnglePoints[2].Y;
                    line2.X2 = MesaureAnglePoints[0].X;
                    line2.Y2 = MesaureAnglePoints[0].Y;
                    canvas.Children.Add(line2);
                }
            }
        }

        void CIEUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Line line4 = new Line();
            line4.StrokeLineJoin = (PenLineJoin)PenLineCap.Round;
            line4.Stroke = new SolidColorBrush(Colors.Blue);
            line4.X1 = axisPoints.MeasureAngleAxis[0].X;
            line4.Y1 = axisPoints.MeasureAngleAxis[0].Y;
            line4.X2 = axisPoints.MeasureAngleAxis[1].X;
            line4.Y2 = axisPoints.MeasureAngleAxis[1].Y;
            canvas.Children.Add(line4);

            Line line5 = new Line();
            line5.StrokeLineJoin = (PenLineJoin)PenLineCap.Round;
            line5.Stroke = new SolidColorBrush(Colors.Blue);
            line5.X1 = axisPoints.MeasureAngleAxis[1].X;
            line5.Y1 = axisPoints.MeasureAngleAxis[1].Y;
            line5.X2 = axisPoints.MeasureAngleAxis[2].X;
            line5.Y2 = axisPoints.MeasureAngleAxis[2].Y;
            canvas.Children.Add(line5);
            Line line6 = new Line();
            line6.StrokeLineJoin = (PenLineJoin)PenLineCap.Round;
            line6.Stroke = new SolidColorBrush(Colors.Blue);
            line6.X1 = axisPoints.MeasureAngleAxis[2].X;
            line6.Y1 = axisPoints.MeasureAngleAxis[2].Y;
            line6.X2 = axisPoints.MeasureAngleAxis[0].X;
            line6.Y2 = axisPoints.MeasureAngleAxis[0].Y;
            canvas.Children.Add(line6);
        }

        private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                axisPoints.BeginDrawAngle = true;
                //axisPoints.NumofMousePress += 1;
                //axisPoints.PressedPoints = e.GetPosition(canvas);
                axisPoints.PressedPoints = axisPoints.ScreenTransformToCIE(e.GetPosition(canvas));
                txtbok.Text = "(" + (Math.Round((axisPoints.PressedPoints.X), 5, MidpointRounding.AwayFromZero)).ToString() +
                         "," + (Math.Round((axisPoints.PressedPoints.Y), 5, MidpointRounding.AwayFromZero)).ToString() + ")";

                Point PointR = axisPoints.TargetPointArray[0];
                Point PointG = axisPoints.TargetPointArray[1];
                Point PointB = axisPoints.TargetPointArray[2];
                //判断三角形三顶点到按下点最近的距离
                //double r, g, b;  //顶点到按下点的距离
                //double min; //顶点到按下点的最小距离
                axisPoints.r = Math.Sqrt(Math.Pow(Math.Abs(axisPoints.TargetPointArray[0].X - (e.GetPosition(canvas)).X), 2) +
                    Math.Pow(Math.Abs(axisPoints.TargetPointArray[0].Y - (e.GetPosition(canvas)).Y), 2));
                axisPoints.g = Math.Sqrt(Math.Pow(Math.Abs(axisPoints.TargetPointArray[1].X - (e.GetPosition(canvas)).X), 2) +
                    Math.Pow(Math.Abs(axisPoints.TargetPointArray[1].Y - (e.GetPosition(canvas)).Y), 2));
                axisPoints.b = Math.Sqrt(Math.Pow(Math.Abs(axisPoints.TargetPointArray[2].X - (e.GetPosition(canvas)).X), 2) +
                    Math.Pow(Math.Abs(axisPoints.TargetPointArray[2].Y - (e.GetPosition(canvas)).Y), 2));
                axisPoints.min = axisPoints.r;
                if (axisPoints.g < axisPoints.min)
                {
                    axisPoints.min = axisPoints.g;
                }
                if (axisPoints.b < axisPoints.min)
                {
                    axisPoints.min = axisPoints.b;
                }
                if (axisPoints.min == axisPoints.r)
                {
                    axisPoints.TargetPointArray[0] = e.GetPosition(canvas);
                }
                else if (axisPoints.min == axisPoints.g)
                {
                    axisPoints.TargetPointArray[1] = e.GetPosition(canvas);
                }
                else if (axisPoints.min == axisPoints.b)
                {
                    axisPoints.TargetPointArray[2] = e.GetPosition(canvas);
                }
                // axisPoints.TargetPointArray[0] = e.GetPosition(canvas);

                canvas.Children.Remove(line0);
                canvas.Children.Remove(line1);
                canvas.Children.Remove(line2);
                //绘制三角形
                line0.Stroke = new SolidColorBrush(Colors.Black);
                //line0.dr
                line0.X1 = axisPoints.TargetPointArray[0].X;
                line0.Y1 = axisPoints.TargetPointArray[0].Y;
                line0.X2 = axisPoints.TargetPointArray[1].X;
                line0.Y2 = axisPoints.TargetPointArray[1].Y;
                canvas.Children.Add(line0);

                //Line line1 = new Line();
                line1.Stroke = new SolidColorBrush(Colors.Black);
                line1.X1 = axisPoints.TargetPointArray[1].X;
                line1.Y1 = axisPoints.TargetPointArray[1].Y;
                line1.X2 = axisPoints.TargetPointArray[2].X;
                line1.Y2 = axisPoints.TargetPointArray[2].Y;
                canvas.Children.Add(line1);
                // Line line2 = new Line();
                line2.Stroke = new SolidColorBrush(Colors.Black);
                line2.X1 = axisPoints.TargetPointArray[2].X;
                line2.Y1 = axisPoints.TargetPointArray[2].Y;
                line2.X2 = axisPoints.TargetPointArray[0].X;
                line2.Y2 = axisPoints.TargetPointArray[0].Y;
                canvas.Children.Add(line2);
            }
        }

        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            axisPoints.PressedPoints = axisPoints.ScreenTransformToCIE(e.GetPosition(canvas));

            txtbok.Text = "(" + (Math.Round((axisPoints.PressedPoints.X), 5, MidpointRounding.AwayFromZero)).ToString() +
                         "," + (Math.Round((axisPoints.PressedPoints.Y), 5, MidpointRounding.AwayFromZero)).ToString() + ")";



            if (true == axisPoints.BeginDrawAngle)
            {

                canvas.Children.Remove(line0);
                canvas.Children.Remove(line1);
                canvas.Children.Remove(line2);
                if (axisPoints.min == axisPoints.r)
                {
                    axisPoints.TargetPointArray[0] = e.GetPosition(canvas);
                }
                else if (axisPoints.min == axisPoints.g)
                {
                    axisPoints.TargetPointArray[1] = e.GetPosition(canvas);
                }
                else if (axisPoints.min == axisPoints.b)
                {
                    axisPoints.TargetPointArray[2] = e.GetPosition(canvas);
                }
                //axisPoints.TargetPointArray[0] = e.GetPosition(canvas);

                //绘制三角形
                line0.Stroke = new SolidColorBrush(Colors.Black);
                //line0.dr
                line0.X1 = axisPoints.TargetPointArray[0].X;
                line0.Y1 = axisPoints.TargetPointArray[0].Y;
                line0.X2 = axisPoints.TargetPointArray[1].X;
                line0.Y2 = axisPoints.TargetPointArray[1].Y;
                canvas.Children.Add(line0);

                line1.Stroke = new SolidColorBrush(Colors.Black);
                line1.X1 = axisPoints.TargetPointArray[1].X;
                line1.Y1 = axisPoints.TargetPointArray[1].Y;
                line1.X2 = axisPoints.TargetPointArray[2].X;
                line1.Y2 = axisPoints.TargetPointArray[2].Y;
                canvas.Children.Add(line1);

                line2.Stroke = new SolidColorBrush(Colors.Black);
                line2.X1 = axisPoints.TargetPointArray[2].X;
                line2.Y1 = axisPoints.TargetPointArray[2].Y;
                line2.X2 = axisPoints.TargetPointArray[0].X;
                line2.Y2 = axisPoints.TargetPointArray[0].Y;
                canvas.Children.Add(line2);
            }
        }

        private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                axisPoints.BeginDrawAngle = false;
            }
        }

        private void canvas_MouseLeave(object sender, MouseEventArgs e)
        {
            axisPoints.BeginDrawAngle = false;
            txtbok.Text = "";
            axisPoints.PressedPoints.X = 0;
            axisPoints.PressedPoints.Y = 0;
        }

        private void canvas_MouseEnter(object sender, MouseEventArgs e)
        {
            axisPoints.PressedPoints = axisPoints.ScreenTransformToCIE(e.GetPosition(canvas));
            txtbok.Text = "(" + (Math.Round((axisPoints.PressedPoints.X), 5, MidpointRounding.AwayFromZero)).ToString() +
                         "," + (Math.Round((axisPoints.PressedPoints.Y), 5, MidpointRounding.AwayFromZero)).ToString() + ")";
        }

    }
}
